#!/bin/bash

#kafka压缩文件名
kafka_file_name=$(ls ./ | grep kafka_.*tgz)
#kafka解压后文件名(截取字符串)
kafka_dir=${kafka_file_name%%.tgz}
#kafka日志目录
kafka_log1="/tmp/kafka-logs1"
kafka_log2="/tmp/kafka-logs2"
kafka_log3="/tmp/kafka-logs3"
echo "------------------------开始读取kafka文件压缩包:${kafka_file_name}------------------------"

echo "------------------------判断文件夹${kafka_dir}是否存在------------------------"
if [ -d "./${kafka_dir}" ];
then
    echo "------------------------文件夹${kafka_dir}已存在,执行删除------------------------"
    rm -rf "./${kafka_dir}"
else
    echo "------------------------文件夹${kafka_dir}不存在,无需删除------------------------"
fi

echo "------------------------开始解压kafka文件压缩包:${kafka_file_name}------------------------"
tar -zxvf ${kafka_file_name}
echo "------------------------完成解压kafka文件压缩包:${kafka_file_name}------------------------"

# 创建log目录
if [ -d "${kafka_log1}" ];
then
    echo "------------------------${kafka_log1}目录已存在,无需创建------------------------"
else
    echo "------------------------${kafka_log1}目录不存在,创建------------------------"
    mkdir "${kafka_log1}"
fi

if [ -d "${kafka_log2}" ];
then
    echo "------------------------${kafka_log2}目录已存在,无需创建------------------------"
else
    echo "------------------------${kafka_log2}目录不存在,创建------------------------"
    mkdir "${kafka_log2}"
fi

if [ -d "${kafka_log3}" ];
then
    echo "------------------------${kafka_log3}目录已存在,无需创建------------------------"
else
    echo "------------------------${kafka_log3}目录不存在,创建------------------------"
    mkdir "${kafka_log3}"
fi


echo "------------------------开始修改配置文件------------------------"
# 创建配置文件
cp "./${kafka_dir}/config/server.properties" "./${kafka_dir}/config/server1.properties"
cp "./${kafka_dir}/config/server.properties" "./${kafka_dir}/config/server2.properties"
cp "./${kafka_dir}/config/server.properties" "./${kafka_dir}/config/server3.properties"
# 修改配置文件
sed  -i  's/broker.id=0/broker.id=1/g'  "./${kafka_dir}/config/server1.properties"
sed  -i  's/broker.id=0/broker.id=2/g'  "./${kafka_dir}/config/server2.properties"
sed  -i  's/broker.id=0/broker.id=3/g'  "./${kafka_dir}/config/server3.properties"
sed  -i  's/log.dirs=\/tmp\/kafka-logs/log.dirs=\/tmp\/kafka-logs1/g'  "./${kafka_dir}/config/server1.properties"
sed  -i  's/log.dirs=\/tmp\/kafka-logs/log.dirs=\/tmp\/kafka-logs2/g'  "./${kafka_dir}/config/server2.properties"
sed  -i  's/log.dirs=\/tmp\/kafka-logs/log.dirs=\/tmp\/kafka-logs3/g'  "./${kafka_dir}/config/server3.properties"

# 修改端口
sed -i '21a port=9091' "./${kafka_dir}/config/server1.properties"
sed -i '21a port=9092' "./${kafka_dir}/config/server2.properties"
sed -i '21a port=9093' "./${kafka_dir}/config/server3.properties"
# 修改zookeeper配置
sed  -i  's/zookeeper.connect=localhost:2181/zookeeper.connect=localhost:2181,localhost:2182,localhost:2183/g'  "./${kafka_dir}/config/server1.properties"
sed  -i  's/zookeeper.connect=localhost:2181/zookeeper.connect=localhost:2181,localhost:2182,localhost:2183/g'  "./${kafka_dir}/config/server2.properties"
sed  -i  's/zookeeper.connect=localhost:2181/zookeeper.connect=localhost:2181,localhost:2182,localhost:2183/g'  "./${kafka_dir}/config/server3.properties"
# 修改分区配置
sed  -i  's/num.partitions=1/num.partitions=3/g'  "./${kafka_dir}/config/server1.properties"
sed  -i  's/num.partitions=1/num.partitions=3/g'  "./${kafka_dir}/config/server2.properties"
sed  -i  's/num.partitions=1/num.partitions=3/g'  "./${kafka_dir}/config/server3.properties"
# 在最后一行插入listeners=PLAINTEXT://10.200.130.79:9091
sed  -i '$a\listeners=PLAINTEXT://10.200.9.43:9091' "./${kafka_dir}/config/server1.properties"
sed  -i '$a\listeners=PLAINTEXT://10.200.9.43:9092' "./${kafka_dir}/config/server2.properties"
sed  -i '$a\listeners=PLAINTEXT://10.200.9.43:9093' "./${kafka_dir}/config/server3.properties"
echo "------------------------结束修改配置文件------------------------"

#echo "------------------------开始启动kafka集群------------------------"
#sh ./${kafka_dir}/bin/kafka-server-start.sh -daemon ./${kafka_dir}/config/server1.properties
#sh ./${kafka_dir}/bin/kafka-server-start.sh -daemon ./${kafka_dir}/config/server2.properties
#sh ./${kafka_dir}/bin/kafka-server-start.sh -daemon ./${kafka_dir}/config/server3.properties
#echo "------------------------结束启动kafka集群------------------------"